﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace System {
    public static partial class Extention {
        /// <summary>
        /// 获取指定类型的实例 
        /// </summary>
        public static T Get<T>(this IConfiguration cfg,string key) where T : class, new() {
            if(cfg==null||string.IsNullOrWhiteSpace(key)) return new T();
            var t = cfg.GetSection(key).Get<T>();
            if(t==null) return new T();
            return t;
        }
        public static TConfig ConfigureStartupConfig<TConfig>(this IServiceCollection services,IConfiguration configuration) where TConfig : class, new() {
            if(services==null)
                throw new ArgumentNullException(nameof(services));
            if(configuration==null)
                throw new ArgumentNullException(nameof(configuration));
            var config = new TConfig();
            configuration.Bind(config);
            services.AddSingleton(config);
            return config;
        }

    }
}
